package com.gs.wzweather.utils;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtil {
    private static Calendar ca = Calendar.getInstance();
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    private static SimpleDateFormat ymdsdf = new SimpleDateFormat("yyyyMMdd");
    public static Map<String, String> getYearMonthDay() {
        Calendar calendar = Calendar.getInstance();
        Date dt=calendar.getTime();
        String reStr = ymdsdf.format(dt);
        Map result = new HashMap();
        result.put("year",reStr.substring(0,4));
        result.put("month",reStr.substring(4,6));
        result.put("day",reStr.substring(6,8));
        return result;
    }

    public static Map<String, String> getYearMonthYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR,0);
        calendar.add(Calendar.MONTH,0);
        calendar.add(Calendar.DAY_OF_YEAR,-1);//日期减1天
        Date dt=calendar.getTime();
        String reStr = ymdsdf.format(dt);
        Map result = new HashMap();
        result.put("year",reStr.substring(0,4));
        result.put("month",reStr.substring(4,6));
        result.put("day",reStr.substring(6,8));
        return result;
    }

    public static Map<String, String> getYearMonthDayBeforeYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR,0);
        calendar.add(Calendar.MONTH,0);
        calendar.add(Calendar.DAY_OF_YEAR,-2);//日期减1天
        Date dt=calendar.getTime();
        String reStr = ymdsdf.format(dt);
        Map result = new HashMap();
        result.put("year",reStr.substring(0,4));
        result.put("month",reStr.substring(4,6));
        result.put("day",reStr.substring(6,8));
        return result;
    }

    /*@Test
    public void  getYearMonthYesterdayTest()throws Exception {
        String str="20170102";
        Date dt=ymdsdf.parse(str);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dt);
        calendar.add(Calendar.YEAR,0);
        calendar.add(Calendar.MONTH,0);
        calendar.add(Calendar.DAY_OF_YEAR,-2);//日期减2天
        Date dt1=calendar.getTime();
        String reStr = ymdsdf.format(dt1);
        System.out.println(reStr);
    }*/

    /*
   获取最新时间
    */
    public static String getTime() {
        String result = null;
        result = sdf.format(new Date());
        result = result.substring(0, 10) + "0000";
        return result;
    }

    public static String subTime(String time, String pastH) {
        String result = null;
        Date newDate;
        try {
            int amount = Integer.parseInt(pastH);
            Date date = sdf.parse(time);
            ca.setTime(date);
            ca.add(Calendar.HOUR_OF_DAY, -amount);
            newDate = ca.getTime();
            result = sdf.format(newDate);
            result = result.substring(0,  10) + "0000";
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return result;
        }
    }
}
